package main;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.plaf.FontUIResource;

//import common.ImageValue;

public class GamePanel extends JPanel implements ActionListener{
	

	private JMenuBar jmb = null;
	
	private GameFrame mainFrame = null;
	
	private GamePanel panel = null;
	
	public final int ROWS=15;//行
	
	public final int COLS=15;//列
	
	private String gameFlag = "start";//游戏状态
	
	//创建指示器二维数组
	Pointer[][] pointers = new Pointer[ROWS][COLS];
	
	//存棋子集合
	public List<QiZi> qizis = new ArrayList<QiZi>();
	//构造方法
	public GamePanel(GameFrame mainFrame) {
		super();
		this.setLayout(null);
		this.setOpaque(false);
		this.mainFrame = mainFrame;
		this.panel = this;
		//图片的加载
		//ImageValue.init();
		//创建菜单
		createMunu();
		//创建鼠标监听
		createMouseListener();
		
		//创建数组内容
		createPointers();
		
		
	}
	
	
	//创建数组内容
	private void createPointers() {
		// TODO Auto-generated method stub
		int x=0;
		int y=0;
		int start=26;
		
		Pointer pointer;
		
		for(int i=0;i<ROWS;i++) {
			for(int j=0;j<COLS;j++) {
				x=j*40+start;
				y=i*40+start;
				pointer = new Pointer(i,j,x,y);
				pointers[i][j]=pointer;
			
		}
			
	}}
	
	//清理之前最后一步的标记
	private void clearAILast() {
		// TODO Auto-generated method stub
		QiZi qizi;
		for(int i=0;i<qizis.size();i++) {
			qizi = qizis.get(i);
			if(qizi!=null&&qizi.getType()==1) {
				qizi.setLast(false);
			}
		}
	}

	//创建鼠标监听
	private void createMouseListener() {
		// TODO Auto-generated method stub
		MouseAdapter mouseAdapter = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if(!"start".equals(gameFlag)) {
					return;
				}
				//绘取鼠标坐标
				int x = e.getX();
				int y = e.getY();
				//循环指示器二维数组
				Pointer pointer;
				for(int i=0;i<ROWS;i++) {
					for(int j=0;j<COLS;j++) {
						//得到每一个指示器对象
						pointer = pointers[i][j];
						if(pointer.isPoint(x,y)&&pointer.getType()==0) {
							QiZi qizi = new QiZi(pointer.getX(),pointer.getY(),2);
							//System.out.println("落子");
							qizis.add(qizi);
							pointer.setType(2);
							
							clearAILast();
							//重绘画布
							repaint();
							//电脑进行一步下棋
							if(AI.has5(pointer,panel)) {//你已经连成功了
								gameWin();
							}else {
								AI.next(panel);
							}
							
							
							return;
						}
					
				}
					
			}
				//重绘画布
				//repaint();
			}
		
			
			@Override
			public void mouseMoved(MouseEvent e) {
				if(!"start".equals(gameFlag)) {
					return;
				}
				//绘取鼠标坐标
				int x = e.getX();
				int y = e.getY();
				//循环指示器二维数组
				Pointer pointer;
				for(int i=0;i<ROWS;i++) {
					for(int j=0;j<COLS;j++) {
						//得到每一个指示器对象
						pointer = pointers[i][j];
						if(pointer.isPoint(x,y)&&pointer.getType()==0) {
							//System.out.println("落子");
							pointer.setShow(true);
						}else {
							pointer.setShow(false);
						}
					
				}
					
			}
				//重绘画布
				repaint();
			}
			
			
	};
		addMouseMotionListener(mouseAdapter);
		addMouseListener(mouseAdapter);
	}

	@Override
	public void paint(Graphics g) {
		
		super.paint(g);
		//定义一个Graphics2D
        //Graphics2D gg = (Graphics2D) g;
       // gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //gg.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
		//绘制网格
		drawGrid(g);
		
		//绘制5个小黑点
		draw5Point(g);
		
		//绘制指示器
		drawPointer(g);
		
		//绘制棋子
		drawQizi(g);
		//画棋子
        //ovalPaint(gg);
	}
	
	//绘制棋子
	private void drawQizi(Graphics g) {
		// TODO Auto-generated method stub
		QiZi qizi;
		for(int i=0;i <qizis.size();i++) {
			qizi=qizis.get(i);
			qizi.draw(g);
		}
	}


	//绘制指示器
	private void drawPointer(Graphics g) {
		// TODO Auto-generated method stub
		Pointer pointer;
		for(int i=0;i<ROWS;i++) {
			for(int j=0;j<COLS;j++) {
				pointer = pointers[i][j];
				if(pointer!=null) {
					pointer.draw(g);
				}
			
		}
			
	}
	}
	
	//绘制5个小黑点
	private void draw5Point(Graphics g) {
		// TODO Auto-generated method stub
		//绘制左上黑点
		int x=142;
		int y=142;
		g.fillArc(x, y, 8, 8, 0, 360);
		
		//绘制右上
		x=462;
		g.fillArc(x, y, 8, 8, 0, 360);
		
		//绘制左下
		x=142;
		y=462;
		g.fillArc(x, y, 8, 8, 0, 360);
		
		//绘制右下
		x=462;
		g.fillArc(x, y, 8, 8, 0, 360);
		
		//绘制中间
		x=302;
		y=302;
		g.fillArc(x, y, 8, 8, 0, 360);
	}
	
	private void drawGrid(Graphics g) {
		// TODO Auto-generated method stub
		int start = 26;
		int x1=26;
		int y1=26;
		int x2=586;
		int y2=26;
		int dis=40;
		
		//绘制15条横线
		for(int i=0;i<ROWS;i++) {
			y1 = i*dis + start;
			y2=y1;
			g.drawLine(x1,y1,x2,y2);
		}
		
		//绘制15条竖线
		y1=26;
		y2=586;
		for(int i=0;i<COLS;i++) {
				x1 = i*dis + start;
				x2=x1;
				g.drawLine(x1,y1,x2,y2);
				}
		
	}
	
	private Font creatFont() {
		return new Font("思源宋体",Font.BOLD,18);
	}
	
	
	//创建菜单
	private void createMunu() {
		// TODO Auto-generated method stub
		//创建JMenuBar
		jmb = new JMenuBar();
		
		//创建字体
		Font tFont = creatFont();
		
		//创建游戏选项
		JMenu jMenu1 = new JMenu("游戏");
		jMenu1.setFont(tFont);
		
		//创建帮助选项
		JMenu jMenu2 = new JMenu("帮助");
		jMenu2.setFont(tFont);
		
		
		JMenuItem jmi1 = new JMenuItem("新游戏");
		jmi1.setFont(tFont);
		JMenuItem jmi2 = new JMenuItem("退出");
		jmi2.setFont(tFont);
		
		//jmi1 jmi2添加到菜单项“游戏”中
		jMenu1.add(jmi1);
		jMenu1.add(jmi2);
		
		JMenuItem jmi3 = new JMenuItem("操作帮助");
		jmi3.setFont(tFont);
		JMenuItem jmi4 = new JMenuItem("胜利条件");
		jmi4.setFont(tFont);
		
		//jmi3 jmi4 添加到菜单项“帮助”中
		jMenu2.add(jmi3);
		jMenu2.add(jmi4);
		
		
		jmb.add(jMenu1);
		jmb.add(jMenu2);
		
		mainFrame.setJMenuBar(jmb);
		
		
		//添加监听
		jmi1.addActionListener(this);
		jmi2.addActionListener(this);
		jmi3.addActionListener(this);
		jmi4.addActionListener(this);
		
		//设置指令
		jmi1.setActionCommand("restart");
		jmi2.setActionCommand("exit");
		jmi3.setActionCommand("help");
		jmi4.setActionCommand("win");
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("菜单子项被点击");
		String commend=e.getActionCommand();
		System.out.println(commend);
		UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("思源宋体",Font.ITALIC,18)));
		UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("思源宋体",Font.ITALIC,18)));
		if("exit".equals(commend)) {
			Object[] options = {"确定","取消"};
			int response = JOptionPane.showOptionDialog(this, "您确定要退出吗", " ",
					 JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,
					 options,options[0]);
			if(response==0) {
				System.exit(0);
			}
		}else if("restart".equals(commend)) {
				if(!"end".equals(gameFlag)) {
					JOptionPane.showMessageDialog(null, "正在游戏中无法重新开始",
							"提示",JOptionPane.INFORMATION_MESSAGE);
				}else {
					restart();
				}
			}else if("help".equals(commend)) {
				JOptionPane.showMessageDialog(null, "鼠标在指示器位置下，则落子！",
						"提示！",JOptionPane.INFORMATION_MESSAGE);
			}else if("win".equals(commend)) {
				JOptionPane.showMessageDialog(null, "五子连珠方获得胜利！",
						"提示！",JOptionPane.INFORMATION_MESSAGE);
			}
	}
	//重新开始
	 void restart() {
		// TODO Auto-generated method stub
		//参数重置
		//1.游戏状态
		//2.指示器
		//3.棋子
		//1.游戏状态
		gameFlag = "start";
		//2.指示器
		Pointer pointer;
		
		for(int i=0;i<ROWS;i++) {
			for(int j=0;j<COLS;j++) {
				
				
				pointer=pointers[i][j];
				pointer.setType(0);
				pointer.setShow(false);
				
			
		}
			
	}
		//3.棋子
		qizis.clear();
	}


	//游戏胜利
	public void gameWin(){
		gameFlag="end";
		//弹出结束提示
		UIManager.put("OptionPane.buttonFont",new FontUIResource(new Font("思源宋体",Font.ITALIC,18)));
		UIManager.put("OptionPane.messageFont",new FontUIResource(new Font("思源宋体",Font.ITALIC,18)));
		JOptionPane.showMessageDialog(mainFrame, "你胜利了，太棒了！");
		
	}
	//游戏结束
		public void gameOver(){
			gameFlag="end";
			//弹出结束提示
			UIManager.put("OptionPane.buttonFont",new FontUIResource(new Font("思源宋体",Font.ITALIC,18)));
			UIManager.put("OptionPane.messageFont",new FontUIResource(new Font("思源宋体",Font.ITALIC,18)));
			JOptionPane.showMessageDialog(mainFrame, "你失败了，请再接再厉！");
			
		}
	

}
